﻿#!/usr/bin/env python3.1
# -*- coding:utf-8 -*-

#Import urllib for HTML page fetching
import urllib.request, urllib.error
#Import regular expression lib for Temp. Data extracting
import re

#Weather data source link
url = r'http://www.google.com.hk/m/search?site=weather&gl=cn&hl=zh-CN&q=%E6%B7%B1%E5%9C%B3'

#Fetch whole HTML Page to String data
try:
    data = urllib.request.urlopen(url).read()
#While URL is illegal
except ValueError:
    print("Bad link. Try to add 'http://' in the front.")
    exit()
#While URLError
except urllib.error.URLError:
    print("Oops! Error ocurs while connecting.")
    exit()

#Search certain pattern and extract Temp. Data
matobj = re.search("&nbsp;(.*)&nbsp;<b>([-]*\d+ \/ [-]*\d+).*&nbsp;<\/b>"
				+ ".*&nbsp;(.*)&nbsp;([-]*\d+ \/ [-]*\d+).*&nbsp;" 
				+ ".*&nbsp;(.*)&nbsp;([-]*\d+ \/ [-]*\d+).*&nbsp;", 
				data.decode('utf-8'))

#If matching not failed
if  None != matobj:
    #Combine extracted Temp. Data
    resultString = matobj.group(1) + matobj.group(2) + '\n' \
				+ matobj.group(3) + matobj.group(4) + '\n' \
				+ matobj.group(5) + matobj.group(6)
#Else
else:
    print('nothing matched')
    exit()
#Endif

print(resultString)
